package com.fengmi.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.fengmi.api.cart.CartApi;
import com.fengmi.api.goods.GoodsApi;
import com.fengmi.cart.Cart;
import com.fengmi.comm.ResultVO;
import com.fengmi.common.filter.UserInfoInterceptor;
import com.fengmi.mapper.OmsOrderItemMapper;
import com.fengmi.mapper.OmsOrderMapper;
import com.fengmi.order.OmsOrder;
import com.fengmi.order.OmsOrderItem;
import com.fengmi.order.dto.OrderDTO;
import com.fengmi.service.IOmsOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fengmi.sso.SysUser;
import com.fengmi.utils.bean.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author sujie
 * @since 2022-12-01
 */
@Service
public class OmsOrderServiceImpl extends ServiceImpl<OmsOrderMapper, OmsOrder> implements IOmsOrderService {
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private OmsOrderItemMapper omsOrderItemMapper;
    @Autowired
    private CartApi cartApi;
    @Autowired
    private GoodsApi goodsApi;
    @Override
    public ResultVO saveOrder(OrderDTO orderDTO) {
      //1 保存订单主表oms_order信息 以及 oms_order_item信息
        if(orderDTO==null || orderDTO.getOmsOrder()==null){
            return  new ResultVO("1001","创建订单失败",null);
        }

        OmsOrder omsOrder = new OmsOrder();
        SysUser userInfo = UserInfoInterceptor.getUserInfo();
        omsOrder.setMemberId(userInfo.getUserId());
        omsOrder.setMemberUsername(userInfo.getAccount());
        omsOrder.setOrderSn(idWorker.nextId()+"");
        omsOrder.setCommentTime(new Date());
        omsOrder.setTotalAmount(orderDTO.getOmsOrder().getTotalAmount());
        omsOrder.setStatus(0); //待付款
        omsOrder.setPayType(2);
        omsOrder.setDeliveryCompany("圆通");
        omsOrder.setReceiverName(orderDTO.getMallAddress().getLocName());
        omsOrder.setReceiverPhone(orderDTO.getMallAddress().getLocTel());
        omsOrder.setReceiverDetailAddress(orderDTO.getMallAddress().getLocInfo());
        this.baseMapper.insert(omsOrder);
        for(Cart cart :orderDTO.getCarts()){
            Cart c = new Cart();
            OmsOrderItem omsOrderItem = new OmsOrderItem();
            omsOrderItem.setOrderId(omsOrder.getId());
            omsOrderItem.setOrderSn(omsOrder.getOrderSn());
            omsOrderItem.setProductSkuId(Long.valueOf(cart.getSkuId()));
            omsOrderItem.setProductPrice(new BigDecimal(cart.getPrice()));
            omsOrderItem.setProductQuantity(cart.getBuyNum());
            omsOrderItemMapper.insert(omsOrderItem);

            //3 商品的扣库存
            goodsApi.kcc( Integer.valueOf(omsOrderItem.getId()+""),c.getBuyNum());

        }
        //2 购物车的扣减
        cartApi.updateCartNew(JSONUtil.toJsonStr(userInfo),orderDTO.getCarts());


        return new ResultVO("1000","订单保存成功",omsOrder.getOrderSn());
    }
}
